package com.ebao.action.doctor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.ebao.action.base.BaseAction;
import com.ebao.constants.WebConstants;
import com.ebao.po.doctor.TDoctor;
import com.ebao.service.doctor.IDoctorService;
import com.ebao.service.talk.ITalkService;
import com.ebao.vo.Talk;

/**
 * @author snow
 */
public class DoctorAction extends BaseAction {

	private static final long serialVersionUID = -4643349923815024953L;
	
	@Resource
	private IDoctorService doctorService;
	@Resource
	private ITalkService talkService;
	
	private String doctorId;
	private String userEmail;
	private String userpass;
	
	private String jsonInfo;
	
	
	/**
	 * 医生登录,跳转至个人首页
	 */
	@Action(value = "login", results = {@Result(name = SUCCESS, location = "doctorhome.jsp"),
			@Result(name = ERROR, location = "/index.jsp")})
	public String login(){
		String jpql = "from TDoctor d where d.email=? and d.password=?";
		TDoctor doctor = doctorService.getByJpql(jpql, userEmail.trim(), userpass.trim());
		if(doctor != null){
			List<Talk> talkList = talkService.getDoctorTalkList(doctor);//医生的说说列表，跳转至医生首页显示
			request.put("talkList", talkList);
			session.put(WebConstants.LOGIN_DOCTOR, doctor);
			// 将登录时间设置进session中，用于获取最新的说说，每次获取以后重新设置该信息
			session.put("loginTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			return SUCCESS;
		} else {
			jsonInfo = "邮箱或密码不匹配";
			return ERROR;
		}
	}

	/**
	 * 医生跳转至设置密码页
	 */
	@Action(value = "goto-setPassword", results = {@Result(name = SUCCESS, 
			location = "doctorset.jsp?fpage=passSet.jsp")})
	public String gotoSetPassword(){
		return SUCCESS;
	}
	
	/**
	 * 医生设置密码
	 */
	@Action(value = "save-password", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root","jsonInfo"})})
	public String savePassword(){
		TDoctor doctor = doctorService.getById(doctorId);
		doctor.setPassword(userpass.trim());
		doctorService.update(doctor);
		jsonInfo = "密码设置成功";
		return SUCCESS;
	}
	
	public String getDoctorId() {
		return doctorId;
	}

	public void setDoctorId(String doctorId) {
		this.doctorId = doctorId;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public String getUserpass() {
		return userpass;
	}

	public void setUserpass(String userpass) {
		this.userpass = userpass;
	}

	public String getJsonInfo() {
		return jsonInfo;
	}

	public void setJsonInfo(String jsonInfo) {
		this.jsonInfo = jsonInfo;
	}

}
